* **************************************************************************** *
* Sierra Leone - Social Signaling and Childhood Immunization                   *
* Aggregate Beliefs                                                            *
* **************************************************************************** *
/*
** Purpose:      Create Table: Aggregate Beliefs

*/
********************************************************************************
********************************************************************************

* AGGREGATE BELIEFS DATA

	use  "${Replicate_SocialSignals_dtaInter}/Endline_Survey_Data.dta", clear


	* merge the ANC treatment status:
	merge m:m clinic  using  "${Replicate_SocialSignals_dtaInter}/MasterData_PilotClinics.dta", gen(ANCtreat)

	* Label the treatment variable:
	lab var treat3   "Signal at 4"
	lab var treat4   "Signal at 5"
	lab var treat2   "Uninformative Bracelet"


	* ----------------------------------------------------------------------------
	** Construct controls:

	* Vaccine 1/BCG:
	replace bcg_endline = 0        if bcg_age>=92&bcg_age!=.
	replace bcg_endline = .        if bcg_endline==1&age_pikin<92
	replace bcg_endline = 0        if age_pikin>=92&bcg_endline==.

	bys  clinic: egen mean_b   = mean(bcg_endline)
	egen mean_meanbcg          = mean(mean_b)
	gen  mean_bcg              = mean_b - mean_meanbcg


	* Vaccine 2/Penta 1:
	replace p1_endline = 0         if penta1_age>=122&penta1_age!=.
	replace p1_endline = .         if p1_endline==1&age_pikin<122
	replace p1_endline = 0         if age_pikin>=122&p1_endline==.

	bys  clinic: egen mean_p1  = mean(p1_endline)
	egen mean_meanp1           = mean(mean_p1)
	gen  mean_penta1           = mean_p1 - mean_meanp1


	* Vaccine 3/Penta 2:
	replace p2_endline = 0         if penta2_age>=153&penta2_age!=.
	replace p2_endline = .         if p2_endline==1&age_pikin<153
	replace p2_endline = 0         if age_pikin>=153&p2_endline==.

	bys  clinic: egen mean_p2  = mean(p2_endline)
	egen mean_meanp2           = mean(mean_p2)
	gen  mean_penta2           = mean_p2 - mean_meanp2


	* Vaccine 4/Penta 3:
	replace p3_endline = 0         if penta3_age>=183&penta3_age!=.
	replace p3_endline = .         if p3_endline==1&age_pikin<183
	replace p3_endline = 0         if age_pikin>=183&p3_endline==.

	bys  clinic: egen mean_p3  = mean(p3_endline)
	egen mean_meanp3           = mean(mean_p3)
	gen  mean_penta3           = mean_p3 - mean_meanp3


	* Vaccine 5/Measles 1:
	replace m1_endline = 0         if measles1_age>=351&measles1_age!=.
	replace m1_endline = .         if m1_endline==1&age_pikin<351
	replace m1_endline = 0         if age_pikin>=351&m1_endline==.

	bys  clinic: egen mean_m1  = mean(m1_endline)
	egen mean_meanm1           = mean(mean_m1)
	gen  mean_measles1         = mean_m1 - mean_meanm1


	* rename the demeaned immunization rates 
	rename mean_bcg         mean_vacc1
	rename mean_penta1      mean_vacc2
	rename mean_penta2      mean_vacc3
	rename mean_penta3      mean_vacc4
	rename mean_measles1    mean_vacc5

	**Exclude caregivers who had trouble understanding 
	tab     outof10_ind
	gen     understood = 1            if inlist(outof10_ind,3,4,5)
	replace understood = 0            if inlist(outof10_ind,1,2)

	drop if understood ==0



	* -----------------------------------------------------------------------------
	* Demean control variables
	tab     arm_anc, gen(anc)

	foreach control in anc2 anc3 anc4  {
		sum    `control' if intervention_arm==1, detail
		gen    `control'_dm     = `control' - `r(mean)'
	}

	tab     education_cat, gen(educ)

	local   ControlVars = " mother_age_w01 educ2 educ3 farm  birth2 birth3 birth4 birth5 "

	foreach control of local  ControlVars  {
		summ `control' if intervention_arm==1, detail
		gen  `control'_dm     = `control' - `r(mean)'
	}

	local   ControlVars = " mother_age_w01_dm educ2_dm educ3_dm farm_dm birth2_dm birth3_dm birth4_dm birth5_dm "
	local   ANC_dm      = " anc2_dm anc3_dm anc4_dm "

	summ  `ControlVars' `ANC_dm'

* **************************************************************************** *
* Aggregate beliefs table 
* **************************************************************************** *


	local  ControlVars = " mother_age_w01_dm educ2_dm educ3_dm farm_dm birth2_dm birth3_dm birth4_dm birth5_dm "
	local  ANC_dm      = " anc2_dm anc3_dm anc4_dm "


	eststo clear
	foreach num in 1 2 3 4 5 {
	areg     rate_vacc`num' treat3 treat4 treat2    `ControlVars' `ANC_dm', absorb(strata) vce(bootstrap , reps(${RepsNum}) seed(${seed}) cluster(clinic))
		eststo
		test   treat2 = treat4
		estadd scalar treat2_treat4 = r(p)
		test   treat2 = treat3
		estadd scalar treat2_treat3 = r(p)
		test   treat3 = treat4
		estadd scalar treat3_treat4 = r(p)
		test   treat2 treat3 treat4
		estadd scalar bracelets     = r(p)
		estadd local Obs            = "`e(N)'"
		estadd scalar C_mean        = _b[_cons]
		estadd local Controls       = "Yes"
		eststo  model_agg_beliefs`num'
	}


	esttab  model_agg_beliefs1 model_agg_beliefs2 model_agg_beliefs3 model_agg_beliefs4 model_agg_beliefs5 ///
	using "${Replicate_SocialSignals_ATables}/Table_AggregateBeliefs_raw.tex",                  ///
	prehead("\begin{tabular}{l*{6}{c}} \toprule "                         ///
	"\multicolumn{1}{c}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{Vaccine 1}} & \multicolumn{1}{c}{\textbf{Vaccine 2}} & \multicolumn{1}{c}{\textbf{Vaccine 3}} & \multicolumn{1}{c}{\textbf{Vaccine 4}} & \multicolumn{1}{c}{\textbf{Vaccine 5}}  \\") ///
	scalars("C_mean           Control Group mean"                         ///
	"Obs              Observations"                               ///
	"treat2_treat3    \(S_{4}\) \(>\) 0: p(UI = S4)"              ///
	"treat2_treat4    \(S_{5}\) \(>\) 0: p(UI = S5)"              ///
	"treat3_treat4    p(S4 = S5)"                                 ///
	"bracelets Joint  F-Test"                                     ///
	"Controls         Controls")                                  ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	varlabels(_cons "Control Group mean")                                 ///
	keep(_cons treat2 treat3 treat4)  nomtitles                           ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                                       ///
	addnotes("") label se  nobaselevels noconstant noobs nolines          ///
	posthead("\midrule") postfoot("\bottomrule \end{tabular}") tex        ///
	replace


	filefilter "${Replicate_SocialSignals_ATables}/Table_AggregateBeliefs_raw.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_AggregateBeliefs_WithControls.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_AggregateBeliefs_raw.tex"





********************************************************************************
** End of the Dofile !!!
********************************************************************************
